package datastructure.search;

/**
 * 线性查找
 *
 * @author jack.wu
 * @version 1.0
 * @date 2020-03-26
 */
public class SeqSearch {
    public static void main(String[] args) {
        int[] array = {5, 7, -5, 6, 8};
        int index = search(array, 6);
        if (index == -1) {
            System.out.println("没查找到~~~~");
        } else {
            System.out.println("找到，下标为：" + index);
        }
    }

    /**
     * 线性查找
     * @param array
     * @param val
     * @return
     */
    private static int search(int[] array, int val) {
        int index = -1;
        for (int i = 0; i < array.length; i++) {
            if (val == array[i]) {
                index = i;
                break;
            }
        }
        return index;
    }

}
